package Logcheck;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import serve.Constant;
import UserFriend.FriendConstant;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class User_log
{
    //用户注册
    public static JSONObject User_SignUp(DatagramPacket datagramPacket)
    {

        JSONObject jsonObject = JSONObject.parseObject(new String(datagramPacket.getData()));
        String username = jsonObject.getString("username");
        String userpasswd = jsonObject.getString("password");

        JSONObject send_json = new JSONObject();
        int code = 0;
        String title ="";
        String message = "";

        JSONObject username_check_json = User_signup_check.CheckUsername(username);
        if(username_check_json.getInteger("code") == CheckConstant.True)
        {
            JSONObject userpasswd_check_json = User_signup_check.CheckUserpasswd(userpasswd);
            if(userpasswd_check_json.getInteger("code") == CheckConstant.True)
            {
                title = CheckConstant.singup_succssed;
                code = CheckConstant.True;
                message = CheckConstant.singup_succssed;
            }
            else
            {
                switch (userpasswd_check_json.getString("message"))
                {
                    case CheckConstant.userpasswd_len_error:
                        title = CheckConstant.userpasswd_len_error;
                        message = CheckConstant.userpasswd_len_message;
                        break;
                    case CheckConstant.userpasswd_chat_error:
                        title = CheckConstant.userpasswd_chat_error;
                        message = CheckConstant.userpasswd_chat_message;
                        break;
                    default:
                        break;
                }
                code = CheckConstant.False;
                //message = userpasswd_check_json.getString("message");
            }
        }
        else
        {
            switch (username_check_json.getString("massage"))
            {
                case CheckConstant.username_len_error:
                    title = CheckConstant.username_len_error;
                    message = CheckConstant.username_len_message;
                    break;
                case CheckConstant.username_chat_error:
                    title = CheckConstant.username_chat_error;
                    message = CheckConstant.username_chat_message;
                    break;
                case CheckConstant.username_exist_error:
                    title = CheckConstant.username_exist_error;
                    message = CheckConstant.username_exist_message;
                    break;
                case CheckConstant.username_first_error:
                    title = CheckConstant.username_first_error;
                    message = CheckConstant.username_first_message;
                    break;
                default:
                    break;
            }

            code = CheckConstant.False;
            //message = username_check_json.getString("message");
        }

        if(code == CheckConstant.True)
        {
            //send_json.put("title",CheckConstant.singup_succssed);

            try
            {
                String sql = "insert into %s(user_name,user_passwd,user_ip,user_state) values(\"%s\",\"%s\",\"%s\",\"%s\")";
                sql = String.format(sql,CheckConstant.usertable,username,userpasswd,datagramPacket.getAddress().toString(),"offline");

                String sql_1 = "insert into %s(user_name) values(\"%s\")";
                sql_1 = String.format(sql_1,FriendConstant.friend_table,username);

                PreparedStatement stat = Constant.chatdbConnector.getChatdb_connection().prepareStatement(sql);
                stat.executeUpdate();
                stat.close();

                stat = Constant.chatdbConnector.getChatdb_connection().prepareStatement(sql_1);
                stat.executeUpdate();
                stat.close();
            }catch (SQLException e)
            {
                e.printStackTrace();
            }
        }

        send_json.put("title",title);
        send_json.put("code",code);
        send_json.put("message",message);

        return send_json;
    }

    //用户登录
    public static JSONObject logIn(DatagramPacket datagramPacket)
    {
        JSONObject jsonObject = JSONObject.parseObject(new String(datagramPacket.getData()));
        String username = jsonObject.getString("username");
        String userpasswd = jsonObject.getString("password");

        JSONObject send_json = new JSONObject();
        int code = 0;
        String message = "";

        boolean user_log_check = false;
        try
        {
            String sql = "select * from %s where user_name=\"%s\" and user_passwd=\"%s\"";
            sql = String.format(sql,CheckConstant.usertable,username,userpasswd);
            Statement stmt = Constant.chatdbConnector.getChatdb_connection().createStatement();
            ResultSet res = stmt.executeQuery(sql);
            user_log_check = res.next();
            if(user_log_check)
            {
                code = CheckConstant.True;
                message = CheckConstant.log_successed;

                sql = "update %s set user_ip=\"%s\",user_state=\"%s\" where user_name=\"%s\" ";
                sql = String.format(sql,CheckConstant.usertable,datagramPacket.getAddress().toString(),CheckConstant.online,username);
                PreparedStatement stat = Constant.chatdbConnector.getChatdb_connection().prepareStatement(sql);
                stat.executeUpdate();
            }
            else
            {
                code = CheckConstant.False;
                message = CheckConstant.log_failed;
            }
        }catch (SQLException e)
        {
            e.printStackTrace();
        }

        if(code == CheckConstant.True)
        {
            send_json.put("title",CheckConstant.log_successed);
        }
        else
        {
            send_json.put("title","登录失败");
        }

        send_json.put("code",code);
        send_json.put("message",message);

        return send_json;
    }
}
